package castle.defense.project;

import java.util.Arrays;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.view.KeyEvent;


public class Options extends PreferenceActivity implements OnPreferenceChangeListener {
	private static final String file = "options", diff = "Difficulty", sound = "Sound", soundMP3S = "SoundMP3";
	public static final int easy = 0, medium = 1, hard = 2;
	private static final String[] difficultyLevels = new String[]{"Easy","Medium","Hard"};
	private static final String highscore = "highscore";
	public static int[] highscores;
	private static boolean internal = false;
	
	static int difficulty;
	static boolean sounds, soundmp3;
	
	static SharedPreferences sp;
	static SharedPreferences.Editor spe;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
	}
	
	public void onResume() {
		SoundManager.setBackground(R.raw.medieval_menu);
		load();
		super.onResume();
	}
	
	public void load() {
		ListPreference difficulties = (ListPreference)this.getPreferenceManager().findPreference(diff);

		difficulties.setEntries(difficultyLevels);
		difficulties.setEntryValues(difficultyLevels);
		difficulties.setValue(sp.getString(diff, difficultyLevels[1]));

		CheckBoxPreference soundcb = (CheckBoxPreference)this.getPreferenceManager().findPreference(sound);
		soundcb.setChecked(sp.getBoolean(sound, true));

		difficulties.setOnPreferenceChangeListener(this);
		soundcb.setOnPreferenceChangeListener(this);
	}
	
	public static void initialize(Context c) {
		sp = c.getSharedPreferences(file, MODE_PRIVATE);
		spe = sp.edit();

		setDiff(sp.getString(diff, difficultyLevels[1]));
		sounds = sp.getBoolean(sound, true);
		soundmp3 = sp.getBoolean(soundMP3S, false);

		highscores = new int[5];
		for(int i = 0; i < highscores.length; i++) {
			highscores[i] = sp.getInt(String.format("%s%d", highscore, i), (i+1)*1000);
		}
		Arrays.sort(highscores);
		for(int i = 0; i < highscores.length/2; i++) {
			int t = highscores[i];
			highscores[i] = highscores[highscores.length-1-i];
			highscores[highscores.length-1-i] = t;
		}
	}
	
	public static void setDiff(String v) {
		for(int i = 0; i < difficultyLevels.length; i++) {
			if(difficultyLevels[i].equals(v)) {
				difficulty = i;
			}
		}
	}

	public static void score(int s) {
		highscores[highscores.length-1] = Math.max(highscores[highscores.length-1], s);
		Arrays.sort(highscores);
		for(int i = 0; i < highscores.length/2; i++) {
			int t = highscores[i];
			highscores[i] = highscores[highscores.length-1-i];
			highscores[highscores.length-1-i] = t;
		}
		for(int i = 0; i < highscores.length; i++) {
			spe.putInt(String.format("%s%d", highscore, i), highscores[i]);
		}
		spe.commit();
	}

	public static boolean soundOn() {return sounds;}
	public static int getDifficulty() {return difficulty;}

	public boolean onPreferenceChange(Preference p, Object k) {
		if(p instanceof ListPreference) {
			ListPreference lp = (ListPreference)p;
			if(lp.getKey().equals(diff)) {
				spe.putString(diff, (String)k);
				setDiff((String)k);
			}
		}

		if(p instanceof CheckBoxPreference) {
			CheckBoxPreference cbp = (CheckBoxPreference)p;
			boolean b = (boolean)(Boolean)k;
			if(cbp.getKey().equals(sound)) {
				spe.putBoolean(sound, b);
				sounds = b;
				//Just in case menu music is still playing.
				if(!sounds) {
					SoundManager.pauseBackground();
				} else {
					if(!SoundManager.resumeBackground())
						SoundManager.setBackground(R.raw.medieval_menu);
				}
			}
		}

		spe.commit();
		return true;
	}

	@Override
	public void onPause() {
		if(!internal) {
			SoundManager.pauseBackground();
		} else {
			internal = false;
		}
		super.onPause();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent e) {
		if(keyCode==KeyEvent.KEYCODE_BACK) {
			Options.internal = true;
		}
		return super.onKeyDown(keyCode, e);
	}
}
